import { upload } from "@/common/utils/uploader/index";
import request from "@/common/utils/request";
import { message } from "ant-design-vue";


const main = {
    // 上传地址
    SetUploadPath:'',

    // 获取上传地址 SetUploadPath
    async getaliinfo() {
        await request
          .get("/work_ai", "/material/upload/info", {
            merchant_folder_statck_id: this.merchant_folder_statck_id,
            upload_from: "",
          })
          .then((res) => {
            if (res.code == 1001) {
            }
            if (res.result) {
              this.SetUploadPath = res.result.upload_path;
            }
          });
      },
    //外层 控制并发数量的工具函数
  processQueue(queue, concurrency) {
       
        return new Promise(async (resolve, rej) => {
            const runningTasks = new Set();
            const results = [];
        
            const addTask = async () => {
              if (queue.length === 0) {
                return;
              }
        
              const task = queue.shift();
              const promise = task()
                .then((res) => {
                  if (res == undefined) {
                    res = false;
                  }
                  results.push(res);
                  runningTasks.delete(promise);
                  addTask();
                })
                  .catch((error) => {
                    console.log(error,'上传失败????');
                    
                  results.push(null);
                  runningTasks.delete(promise);
                  addTask();
                });
        
              runningTasks.add(promise);
            };
        
            for (let i = 0; i < concurrency; i++) {
              addTask();
            }
        
            while (runningTasks.size > 0 || queue.length > 0) {
              await Promise.race(runningTasks);
            }
            resolve()
      })
   
  },

  // Oss上传
    startOssUploads(params = {}) {
      const { files, } = params
      
      this.merchant_folder_statck_id = files?.[0]?.merchant_folder_statck_id

        const Success_list = []
        return new Promise(async (resolve, reject) => {
            try {
            // 获取oss 上传地址: SetUploadPath
            await this.getaliinfo()

            //  这里是循环这个  待上传的数组    循环 调用oss上传的方法了 但之前上传的是  file文件 这里可能要改的
          const uploadQueue = files.map((item, index) => async () => {
            const res = await upload(item?.file, { SetUploadPath: this.SetUploadPath }, {
              parent_id: item.parent_id,
              sort: item.sort,
            });
      
            // -----------------------这里就是  oss上传成功了 然后上传给后端了  ----------------------------- 
            // res.fileUrl   上传后传出来的  预览地址
            Success_list.push(res);
          });
           
          const results = await this.processQueue(uploadQueue, 2);   //  这个是利用工具函数控制大的并发量   一次可以执行 2次  oss的上传
          // 之前的逻辑这里请求后端地址   返回这个response  然后判断results里面每个result都为true  就是上传完成了这个你具体看
            
          resolve(Success_list)
            } catch (error) {
                message.error('图片上传失败')
                reject()
            }
           
        })

    }
    
}


export default main
